<?php
	require_once($_SERVER["DOCUMENT_ROOT"]."/global.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/security_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/channel_mod.php");

	SecurityMod::login();

	$level = $_SESSION["account"]->getLevel();
        $chatLevel = $_SESSION["account"]->getChatLevel();
	if ($level != "Admin" && $chatLevel != 3)
		SecurityMod::logout();

        $id = v($_REQUEST, "id");
        $channel = ChannelMod::getChannel($id);
        $unsignedAccounts = ChannelMod::getUnsignedUsers($id);
        $channelUsers = ChannelMod::getChannelUsers($id);
?>
<html>
<head>
<title><?php echo(SettingsMod::PAGE_TITLE." :: Channel Users"); ?></title>
<link rel="stylesheet" href="main.css">
<script language="javascript"> 
        function move(tbFrom, tbTo) {
            var arrFrom = new Array(); var arrTo = new Array(); 
            var arrLU = new Array();
            var i;
            for (i = 0; i < tbTo.options.length; i++) 
            {
            arrLU[tbTo.options[i].text] = tbTo.options[i].value;
            arrTo[i] = tbTo.options[i].text;
            }
            var fLength = 0;
            var tLength = arrTo.length;
            for(i = 0; i < tbFrom.options.length; i++) 
            {
            arrLU[tbFrom.options[i].text] = tbFrom.options[i].value;
            if (tbFrom.options[i].selected && tbFrom.options[i].value != "") 
            {
            arrTo[tLength] = tbFrom.options[i].text;
            tLength++;
            }
            else 
            {
            arrFrom[fLength] = tbFrom.options[i].text;
            fLength++;
            }
            }

            tbFrom.length = 0;
            tbTo.length = 0;
            var ii;

            for(ii = 0; ii < arrFrom.length; ii++) 
            {
            var no = new Option();
            no.value = arrLU[arrFrom[ii]];
            no.text = arrFrom[ii];
            tbFrom[ii] = no;
            }

            for(ii = 0; ii < arrTo.length; ii++) 
            {
            var no = new Option();
            no.value = arrLU[arrTo[ii]];
            no.text = arrTo[ii];
            tbTo[ii] = no;
            }
        }
        
        function selectAllAndSubmit(box){
            for(var i=0;i<box.length;i++){
                box.options[i].selected = true;
            }
            document.forms["userForm"].submit();
        }
</script>
</head>
<body>
    <h2 align="center">Channel Users - <?php echo($channel->getName())?></h2>
	<table align="center" width="400">
	<tr>
		<td align="center">
			<table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align="center" width="100%">
			<tr>
				<th><u>Accounts</u></th>
                                <th><u>Add/remove</u></th>
                                <th><u>Channel users</u></th>
			</tr>
			<tr>
                                <td align="center">
                                    <select multiple size="15" name="accounts[]" id="account" style="width:150">
                                        <?php foreach ($unsignedAccounts as $usa): ?>
                                            <option value="<?php echo($usa->getId()); ?>"><?php echo($usa->getName()); ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </td>
				<td align="center">
                                    <input type="button" onClick="move(document.getElementById('account'),document.getElementById('channelUser'))" value="->"><br />
                                    <input type="button" onClick="move(document.getElementById('channelUser'),document.getElementById('account'))" value="<-">
                                </td>
                                <form method="POST" action="channel_users_update.php" id="userForm">
                                <td align="center">
                                    <select multiple="multiple" size="15" name="channelUsers[]" id="channelUser" style="width:150">
                                        <?php foreach ($channelUsers as $cu): ?>
                                            <option value="<?php echo($cu->getId()); ?>"><?php echo($cu->getName()); ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </td>
			</tr>
                        <tr>
                            <td colspan="3" align="center">&nbsp</td>
                        </tr>
                        <tr>
                            <td colspan="3" align="center">
                                <input type="hidden" name="id" value="<?php echo($channel->getId()); ?>" />
                                <input type="button" value="Cancel" onclick="history.go(-1);" /><br /><br />
                                <input type="button" value="Save" onclick="selectAllAndSubmit(document.getElementById('channelUser'))"
                                
                            </td>
                            </form>
                        </tr>
		</td>
	</tr>
	</table>
</body>
</html>
